package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.dilidili_ting.R;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.LikeBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.sackcentury.shinebuttonlib.ShineButton;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MovieDetailsActivityTing extends AppCompatActivity {

    private ImageView ivMoviedetailTing;
    private TextView tvMovietitleTing;
    private TextView tvMovieyearTing;
    private TextView tvMoviecountryTing;
    private TextView tvMoviedirectorTing;
    private Button playBtnTing;
    private TextView tvMoviejianjieTing;
    private TextView tvPerformer1Ting;
    private String movieName_ting;
    private RelativeLayout llBarTing;
    private ImageView ivBackTing;
    private TextView tvTitleTing;
    private ShineButton btnShoucangTing;
    private DataBeanTing<List<MovieBeanTing>> r;
    private MovieBeanTing movieBeanTing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_details_ting);
        movieName_ting = getIntent().getStringExtra("movieName");
        initView();
        getData();
    }

//    初始化控件
    private void initView() {
        btnShoucangTing = (ShineButton) findViewById(R.id.btn_shoucang_ting);
        llBarTing = (RelativeLayout) findViewById(R.id.ll_bar_ting);
        ivBackTing = (ImageView) findViewById(R.id.iv_back_ting);
        tvTitleTing = (TextView) findViewById(R.id.tv_title_ting);
        ivMoviedetailTing = (ImageView) findViewById(R.id.iv_moviedetail_ting);
        tvMovietitleTing = (TextView) findViewById(R.id.tv_movietitle_ting);
        tvMovieyearTing = (TextView) findViewById(R.id.tv_movieyear_ting);
        tvMoviecountryTing = (TextView) findViewById(R.id.tv_moviecountry_ting);
        tvMoviedirectorTing = (TextView) findViewById(R.id.tv_moviedirector_ting);
        playBtnTing = (Button) findViewById(R.id.play_btn_ting);
        tvMoviejianjieTing = (TextView) findViewById(R.id.tv_moviejianjie_ting);
        tvPerformer1Ting = (TextView) findViewById(R.id.tv_performer1_ting);
        tvTitleTing.setText("电影详情");
//        返回按钮
        ivBackTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MovieDetailsActivityTing.this.finish();
            }
        });
//        播放按钮
        playBtnTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addhistory(r.data.get(0));
                Intent i = new Intent(MovieDetailsActivityTing.this,VideoPlayActivityTing.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("movieData", r.data.get(0));//传入类的对象
                i.putExtras(bundle);
                startActivity(i);
            }
        });
//        收藏按钮点击事件
        btnShoucangTing.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(View view, boolean checked) {
                if(checked){
                    shoucang("favorite/addFavorite");
                }else {
                    shoucang("favorite/cancelFavorite");
                }
            }
        });
    }

//    收藏点击事件
    private void shoucang(String url){
        JsonObject jo = new JsonObject();
        jo.addProperty("userId",SharedPreferencesUtilsTing.readLoginUserID(this));
        jo.addProperty("movieId",movieBeanTing.movieId);
        OkhttpUtilsTing.isPOST(url, true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {}
        });
    }

//    请求添加播放记录的接口
    private void addhistory(MovieBeanTing movieBeanTing){
        JsonObject jo = new JsonObject();
        jo.addProperty("userId", SharedPreferencesUtilsTing.readLoginUserID(this));
        jo.addProperty("movieId",movieBeanTing.movieId);
        OkhttpUtilsTing.isPOST("playHistory/addPlayHistory", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {}
        });
    }

//    获取接口电影详情数据
    private void getData(){
        JsonObject jo = new JsonObject();
        jo.addProperty("movieName",movieName_ting);
        OkhttpUtilsTing.isPOST("movie/getMovies", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                r = gson.fromJson(res, new TypeToken<DataBeanTing<List<MovieBeanTing>>>(){}.getType());
                MovieDetailsActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setData(r.data);
                        likestatu(r.data.get(0).movieId);
                    }
                });
            }
        });
    }

//    查询收藏状态
    private void likestatu(Integer id){
        JsonObject jo = new JsonObject();
        jo.addProperty("userId",SharedPreferencesUtilsTing.readLoginUserID(this));
        OkhttpUtilsTing.isPOST("favorite/getFavoriteByUserId", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<MovieBeanTing>> l= gson.fromJson(res, new TypeToken<DataBeanTing<List<MovieBeanTing>>>(){}.getType());
                MovieDetailsActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i=0;i<l.data.size();i++){
                            if(id.equals(l.data.get(i).movieId)){
                                btnShoucangTing.setChecked(true);
                            }
                        }
                    }
                });
            }
        });
    }

//    将数据渲染到页面上
    private void setData(List<MovieBeanTing> data) {
        movieBeanTing = data.get(0);
        tvMovietitleTing.setText(movieBeanTing.movieName);
        //加载网络图片
        RequestOptions options = new RequestOptions().error(R.drawable.k8).bitmapTransform(new RoundedCorners( 10 ));//图片圆角为10
        Glide.with(ivMoviedetailTing.getContext()).load(movieBeanTing.movieCover).apply(options).into(ivMoviedetailTing);
        tvMovieyearTing.setText(movieBeanTing.releaseYear+"年上映");
        tvMoviecountryTing.setText("电影 "+movieBeanTing.country);
        tvMoviedirectorTing.setText("导演 "+movieBeanTing.director);
        tvMoviejianjieTing.setText("\u3000\u3000"+movieBeanTing.movieIntro);
        tvPerformer1Ting.setText("\u3000\u3000"+movieBeanTing.cast);
    }


}